package com.appbee.cm.client.startup;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Provider;

import com.appbee.core.client.startup.StartupChain;
import com.appbee.core.client.startup.StartupChainBuilder;
import com.appbee.core.client.startup.StartupLink;
import com.appbee.core.client.startup.impl.StartupChainBuilderImpl;
import com.appbee.core.client.startup.links.HistoryHandlerStartupLink;
import com.appbee.core.client.startup.links.RemoveLoadingElementLink;
import com.appbee.core.client.startup.links.ShellLink;
import com.appbee.core.client.startup.links.StartupDataLoaderLink;
import com.appbee.user.client.startup.UserStartupLink;
import com.google.gwt.user.client.ui.HasWidgets.ForIsWidget;

public class CMStartupChain implements StartupChain
{
	private final List<Provider<? extends StartupLink>> linksBeforeDataLoad = new ArrayList<Provider<? extends StartupLink>>();
	private final List<Provider<? extends StartupLink>> linksAfterDataLoad = new ArrayList<Provider<? extends StartupLink>>();

	@Inject
	public CMStartupChain(	Provider<UserStartupLink> userLinkProvider,
							Provider<CMStarupLink> cmLinkProvider,
							Provider<StartupDataLoaderLink> startupDataLoaderLinkProvider,
							Provider<HistoryHandlerStartupLink> historyHandlerLink,
							Provider<AdminPanelLink> adminPanelLinkProvider,
							Provider<ShellLink> shellLinkProvider,
							Provider<RemoveLoadingElementLink> removeLoadingElLinkProvider)
	{
		linksBeforeDataLoad.add(userLinkProvider);
		linksBeforeDataLoad.add(cmLinkProvider);
		linksAfterDataLoad.add(startupDataLoaderLinkProvider);
		linksAfterDataLoad.add(historyHandlerLink);
		linksAfterDataLoad.add(adminPanelLinkProvider);
		linksAfterDataLoad.add(shellLinkProvider);
		linksAfterDataLoad.add(removeLoadingElLinkProvider);
	}

	public CMStartupChain chainBeforeDataload(Provider<? extends StartupLink> startupLinkProvider)
	{
		linksBeforeDataLoad.add(startupLinkProvider);
		return this;
	}

	public CMStartupChain chainAfterDataload(Provider<? extends StartupLink> startupLinkProvider)
	{
		linksAfterDataLoad.add(startupLinkProvider);
		return this;
	}

	@Override
	public void start(ForIsWidget container)
	{
		StartupChainBuilder chainBuilder = new StartupChainBuilderImpl();
		for (Provider<? extends StartupLink> startupLinkProvider : linksBeforeDataLoad)
		{
			chainBuilder.chainProvider(startupLinkProvider);
		}
		for (Provider<? extends StartupLink> startupLinkProvider : linksAfterDataLoad)
		{
			chainBuilder.chainProvider(startupLinkProvider);
		}

		chainBuilder.getChain().start(container);
	}

}
